//
//  ICEEMUrlScheme.m
//  EasyMoney
//
//  Created by 邓乐 on 2020/1/9.
//  Copyright © 2020 zh. All rights reserved.
//

#import "ICEEMUrlScheme.h"
#import "ICEJavaScriptBridge.h"
#import "ICENavigator.h"

@interface ICEEMUrlScheme()
{
    EMSuccessCallbackBlock _success;
    EMFailedCallbackBlock _failed;
}
@end

@implementation ICEEMUrlScheme

-(void)openUrl:(NSDictionary*)args
{
    NSDictionary* param = [args objectForKey:@"param"];
    NSString* url = [param objectForKey:@"url"];
    if (SAFE_STRING(url).length < 4) {
        NSDictionary* ret = @{
                              @"status":@"error_format",
                              };
        [ICEJavaScriptBridge sendReslutToJS:ret callInfo:args];
        return;
    }
    
    _success  = ^(int result, id params){

        NSMutableDictionary* dit = [[NSMutableDictionary alloc]init];
        [dit setObject:@"success" forKey:@"status"];
        if ([params isKindOfClass:[NSDictionary class]]) {
            [dit addEntriesFromDictionary:params];
        }
        [ICEJavaScriptBridge sendReslutToJS:dit callInfo:args];
        
    };
    
    _failed  = ^(int result, id params){
        NSMutableDictionary* dit = [[NSMutableDictionary alloc]init];
        [dit setObject:@"failed" forKey:@"status"];
        if ([params isKindOfClass:[NSDictionary class]]) {
            [dit addEntriesFromDictionary:params];
        }
        [ICEJavaScriptBridge sendReslutToJS:dit callInfo:args];
    };
    
    dispatch_delay_start(0.5)
    [ICENavigator openURLString:SAFE_STRING(url) fromViewController:nil initAfterAlloc:^id(UIViewController *vc) {
        
        return @{
                 @"token":@"1",
                 @"successBlock":self->_success,
                 @"failedBlock":self->_failed
                 };
    }];
    dispatch_delay_end
}

@end
